package com.git.smp.controller;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.dom4j.Document;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.git.smp.core.ResponseMessage;
import com.git.smp.dao.SmpFieldMapper;
import com.git.smp.entity.SmpField;
import com.git.smp.entity.SmpSystemCallExtend;
import com.git.smp.utils.FieldUtil;
import com.git.smp.utils.InitMappingFile;


@RestController
@RequestMapping(value = "/serviceExample")
public class Service2XmlController {
	 Logger logger = LoggerFactory.getLogger(InitMappingFile.class);
	//private SmpServiceMapper serviceMapper;
		@Autowired
		private HttpServletResponse response;
	@Autowired
	private SmpFieldMapper fieldMapper;

	
		@RequestMapping("/service2Xml")
		public ResponseMessage<String> service2Xml(@RequestBody SmpSystemCallExtend smpSystemCallExtend
				
				) {
			ResponseMessage<String> responseMessage = new ResponseMessage<String>();
			responseMessage.setCode("0000");
			responseMessage.setMessage("查询成功！");
			
			SmpField field = new SmpField();
			field.setTransCode(smpSystemCallExtend.getTransCode());
			field.setSystemNo(smpSystemCallExtend.getRegSysCode());
			
			
			List<SmpField> smpFieldList =  fieldMapper.getFieldByServer(field);
			
			Document doc = FieldUtil.fieldToXml(smpFieldList, smpSystemCallExtend);
			//doc.a
			
			OutputFormat format = OutputFormat.createPrettyPrint();
			// 设置输出编码
			format.setEncoding("UTF-8");
			// 去除空行
			format.setNewLineAfterDeclaration(true);
			format.setSuppressDeclaration(true);

			// 输出文件
			// File file = new File(tarPath +File.separator+ system + "_" + pubSrvCode +
			// ".xml");
			ByteArrayOutputStream bo = new ByteArrayOutputStream();
			XMLWriter writer = null;
			try {
				writer = new XMLWriter(bo,format);
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				writer.write(doc);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			String result = null;
			try {
				 result = bo.toString("UTF-8");
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			logger.info(result);
			responseMessage.setExtend(result.replaceFirst("\n", ""));
			
			return responseMessage;
		}
		
		
		@RequestMapping("/service2Json")
		@ResponseBody
		public ResponseMessage<Map> service2Json(@RequestBody SmpSystemCallExtend smpSystemCallExtend
				) {
			ResponseMessage<Map> responseMessage = new ResponseMessage<Map>();
			responseMessage.setCode("0000");
			responseMessage.setMessage("查询成功！");
			response.setContentType("application/json;charset=UTF-8");
			
			SmpField field = new SmpField();
			field.setTransCode(smpSystemCallExtend.getTransCode());
			field.setSystemNo(smpSystemCallExtend.getRegSysCode());
			
			List<SmpField> smpFieldList =  fieldMapper.getFieldByServer(field);
			
			Map doc = FieldUtil.fieldToJson(smpFieldList,smpSystemCallExtend);
			
			//GsonJsonParser
			/*
			 * XmlMapper xmlMapper = new XmlMapper(); ObjectMapper objectMapper = new
			 * ObjectMapper(); JsonNode jsonNode =null; try { jsonNode =
			 * xmlMapper.readTree(doc.asXML()); } catch (JsonMappingException e1) { // TODO
			 * Auto-generated catch block e1.printStackTrace(); } catch
			 * (JsonProcessingException e1) { // TODO Auto-generated catch block
			 * e1.printStackTrace(); } String result = ""; try { result =
			 * objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonNode); }
			 * catch (JsonProcessingException e) { // TODO Auto-generated catch block
			 * e.printStackTrace(); }
			 */
			responseMessage.setExtend(doc);
			
			return responseMessage;
		}
}
